package surveyBusiness;

import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.DDAlert;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/6/10.
 */
public class GetSurveyList implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
//        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();


        //数据库操作
        String action = request.getParameter("action");

        //获取问卷 名称 列表
        if (Objects.equals("getList", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "SELECT id, surveyname FROM survey WHERE type=1 ORDER BY createtime DESC";
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.insert(sql);
            returnJson.setResult(sqlReturnModel.getSelectReturnData());
        }
        //获取可用的问卷 SELECT surveyuniqueid FROM surveyuser WHERE state=0 AND surveyid=9
        if (Objects.equals("getAvailableSurvey", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String surveyId = request.getParameter("surveyId");
            String sql = "SELECT surveyuniqueid FROM surveyuser WHERE state=0 AND surveyid="+surveyId;
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            returnJson.setResult(sqlReturnModel.getSelectReturnData());
        }
        //是否推送钉钉
        if (Objects.equals("sendDD", action)){
            String surveyId = request.getParameter("surveyId");
            String uniqueId = request.getParameter("uniqueId");
            String pro = request.getParameter("pro");

            String domain = myConfig.getPropertiesConfig().getProperties().getProperty("domain");
            String port = myConfig.getPropertiesConfig().getProperties().getProperty("port");
            //yxm.vaiwan.com        10.10.28.42:1985

//            String domainUrl = "http://yxm.vaiwan.com/html/surveyTemplate.html?";
            String domainUrl = "http://"+domain+port+"/html/surveyTemplate.html?";
            domainUrl = domainUrl + "uniqueid="+uniqueId+"&surveyid="+surveyId;
            String picUrl = "https://efuli.oss-cn-hangzhou.aliyuncs.com/pic/b6837786708dfd1c85101ce16c5865bd";
            String name = "易福励【"+pro+"】反馈调查问卷";    //这里编辑项目名
            String[] strings = {"[mu]通过迭代后对最终客户的反馈意见，以便优化改进后期开发测试的工作",name,picUrl, domainUrl};
            DDAlert ddAlert = new DDAlert();
            ddAlert.send("link", "url3", strings, "");
        }

        return returnJson.toString();
    }


}
